﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ETL.Business.Models;
using ETL.Common.Logging;
using ETL.DataAccess;
using ETL.Entity;

namespace ETL.Business.Handlers.ProjectActualResourceHandler
{
    public class GetActualResourceUsage
    {

        private readonly ProjectActualResourceUsageRepository _projectActualResourceUsageRepository;

        public GetActualResourceUsage(ProjectActualResourceUsageRepository ProjectActualResourceUsageRepository)
        {
            _projectActualResourceUsageRepository = ProjectActualResourceUsageRepository;
        }
        public GetActualResourceUsage()
        {
            _projectActualResourceUsageRepository = new ProjectActualResourceUsageRepository();
        }
        public virtual IEnumerable<ProjectActualResourceUsageModel> Execute(decimal projectId, decimal taskId)
        {
            // try to get from db
            try
            {
                IEnumerable<ProjectActualResourceUsageModel> result = null;
                // convert
                var projectActualResourceUsageData =
                    _projectActualResourceUsageRepository.GetListActualResourceUsage(projectId, taskId);
                if (projectActualResourceUsageData != null && projectActualResourceUsageData.Count() > 0)
                {
                    return from s in projectActualResourceUsageData select new ProjectActualResourceUsageModel(s);
                }
                return result;
            }
            catch (Exception ex)
            {

                LogService.Service.Debug(ex);
                return null;
            }


        }

        public virtual ProjectActualResourceUsageModel Execute (decimal genActualResourceUsageId)
        {
            // try to get from db
            try
            {
                ProjectActualResourceUsageModel result = null;
                // convert
                var projectActualResourceUsageData =
                    _projectActualResourceUsageRepository.GetActualResourceUsage(genActualResourceUsageId);
                if (projectActualResourceUsageData != null)
                {
                    return new ProjectActualResourceUsageModel(projectActualResourceUsageData);
                }
                return result;
            }
            catch (Exception ex)
            {

                LogService.Service.Debug(ex);
                return null;
            }
        }
    }
}
